home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
filesyst
/
dosfs
/
dmsdosfs.000
/
dmsdosfs
/
dmsdosfs-0.6.9b
/
dpatch.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-17
|
3KB
|
146 lines
/* dpatch.c
A trial to develop an intelligent patch program
(failed- but it's enough for dmsdos installation)
(Using diffs, I didn't manage to maintain 1.2 *and* 1.3 kernel support
without having to write two completely different packages :-( )
*/
#include<stdio.h>
#include<string.h>
#define MAXLEN 500
int readline(FILE*f,char*text,int max)
{ int i;
int c;
i=0;
while((c=fgetc(f))!=EOF&&c!='\n'&&i<max)
{ text[i]=c;
++i;
}
text[i]='\0';
if(c==EOF&&text[0]=='\0')return EOF;
return i;
}
void writeline(FILE*f,char*text)
{
fprintf(f,"%s\n",text);
}
int xcmp(char*xa,char*xb)
{ int i;
char sa[MAXLEN];
char sb[MAXLEN];
char * a;
char * b;
strcpy(sa,xa);
strcpy(sb,xb);
a=sa;
b=sb;
while(*a==' '||*a=='\t')++a;
while(*b==' '||*b=='\t')++b;
i=strlen(a)-1;
while(i>=0&&(a[i]==' '||a[i]=='\t')){a[i]='\0';--i;}
i=strlen(b)-1;
while(i>=0&&(b[i]==' '||b[i]=='\t')){b[i]='\0';--i;}
return strcmp(a,b);
}
int main(int argc,char*argv[])
{ FILE*in;
FILE*out;
FILE*pat;
int a;
int i;
char patchstring[MAXLEN];
char actual[MAXLEN];
if(argc!=4)
{ printf("DPATCH 0.1.1 (C) 1995 Frank Gockel\n\n");
printf("Usage: dpatch infile outfile patchfile\n");
return 1;
}
in=fopen(argv[1],"r");
if(in==NULL)
{ printf("Error: File %s not found.\n",argv[1]);
return 2;
}
pat=fopen(argv[3],"r");
if(pat==NULL)
{ printf("Error: File %s not found.\n",argv[3]);
fclose(in);
return 2;
}
out=fopen(argv[2],"w");
if(out==NULL)
{ printf("Error: Could not write file %s.\n",argv[2]);
fclose(in);
fclose(pat);
return 3;
}
printf("Patching %s ...",argv[2]);
readline(in,actual,MAXLEN);
while(readline(pat,patchstring,MAXLEN)!=EOF)
{ if(strncmp(patchstring,"find ",5)==0)
{ while(xcmp(patchstring+5,actual)!=0)
{ writeline(out,actual);
if(readline(in,actual,MAXLEN)==EOF)
{ printf("Error: %s not found in %s.\n",patchstring+5,argv[1]);
fclose(in);
fclose(out);
fclose(pat);
return 4;
}
}
}
else if(strncmp(patchstring,"delete",6)==0)
{ a=1;
sscanf(patchstring+6,"%d",&a);
for(i=0;i<a;++i)readline(in,actual,MAXLEN);
}
else if(strncmp(patchstring,"insert ",7)==0)
{ writeline(out,actual);
strcpy(actual,patchstring+7);
}
else if(strncmp(patchstring,"replace ",8)==0)
{ strcpy(actual,patchstring+8);
}
else if(strncmp(patchstring,"next",4)==0)
{ a=1;
sscanf(patchstring+4,"%d",&a);
for(i=0;i<a;++i)
{ writeline(out,actual);
readline(in,actual,MAXLEN);
}
}
else if(xcmp(patchstring,"")==0); /* ignore empty line */
else if(*patchstring=='#'); /* ignore comment line */
else
{ printf("Error: Syntax error in patchfile %s:\n%s\n",argv[3],patchstring);
fclose(in);
fclose(out);
fclose(pat);
return 5;
}
}
writeline(out,actual);
while(readline(in,actual,MAXLEN)!=EOF)writeline(out,actual);
fclose(in);
fclose(pat);
fclose(out);
printf("ok.\n");
return 0;
}